package com.zlkj.pro.modular.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.admin.model.dto.ResourcesAdminDTO;
import com.zlkj.pro.modular.admin.model.vo.ResourcesAdminVO;
import com.zlkj.pro.modular.admin.service.ResourcesAdminService;
import com.zlkj.pro.modular.common.entity.ResourcesEntity;
import com.zlkj.pro.modular.common.mapper.ResourcesMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 孙铭泽
 * @date 2022/9/23
 * @time 20:12
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ResourcesAdminServiceImpl extends ServiceImpl<ResourcesMapper, ResourcesEntity> implements ResourcesAdminService {

    @Override
    public PageResult<ResourcesAdminVO> findPage(ResourcesAdminDTO resourcesAdminDTO) {
        Integer pageNo = resourcesAdminDTO.getPageNo();
        Integer pageSize = resourcesAdminDTO.getPageSize();
        String title = resourcesAdminDTO.getTitle();
        LambdaQueryWrapper<ResourcesEntity> resourcesEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        resourcesEntityLambdaQueryWrapper.like(StrUtil.isNotBlank(title),ResourcesEntity::getTitle,title);
        Page<ResourcesEntity> resourcesEntityPage = super.page(new Page<>(pageNo, pageSize), resourcesEntityLambdaQueryWrapper);
        long total = resourcesEntityPage.getTotal();
        List<ResourcesEntity> records = resourcesEntityPage.getRecords();
        List<ResourcesAdminVO> resourcesAdminList = ConvertUtil.turn(records, ResourcesAdminVO.class);
        return PageResultFactory.createPageResult(resourcesAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<ResourcesAdminVO> findList(ResourcesAdminDTO resourcesAdminDTO) {
        LambdaQueryWrapper<ResourcesEntity> resourcesEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        List<ResourcesEntity> resourcesEntityList = super.list(resourcesEntityLambdaQueryWrapper);
        return ConvertUtil.turn(resourcesEntityList, ResourcesAdminVO.class);
    }

    @Override
    public ResourcesAdminVO findDetail(ResourcesAdminDTO resourcesAdminDTO) {
        Long id = resourcesAdminDTO.getId();
        LambdaQueryWrapper<ResourcesEntity> resourcesEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        resourcesEntityLambdaQueryWrapper.eq(ResourcesEntity::getId, id);
        ResourcesEntity resourcesEntity = super.getOne(resourcesEntityLambdaQueryWrapper, Boolean.FALSE);
        return ConvertUtil.turn(resourcesEntity, ResourcesAdminVO.class);
    }

    @Override
    public boolean add(ResourcesAdminDTO resourcesAdminDTO) {
        ResourcesEntity resourcesEntity = ConvertUtil.turn(resourcesAdminDTO, ResourcesEntity.class);
        return super.save(resourcesEntity);
    }

    @Override
    public boolean edit(ResourcesAdminDTO resourcesAdminDTO) {
        ResourcesEntity resourcesEntity = ConvertUtil.turn(resourcesAdminDTO, ResourcesEntity.class);
        return super.updateById(resourcesEntity);
    }

    @Override
    public boolean delete(ResourcesAdminDTO resourcesAdminDTO) {
        return super.removeById(resourcesAdminDTO);
    }

}
